

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Furienson
 */
public class Anuncio{
    protected int id=0;
    protected String titulo="";
    protected String texto="";
    protected float precio=0;
    protected boolean vendido=false;

    
     public void setId(int id) {
        this.id=id;
    }
    public int getId() {
        return id;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public String getTexto() {
        return texto;
    }

    public void setTexto(String texto) {
        this.texto = texto;
    }

    public float getPrecio() {
        return precio;
    }

    public void setPrecio(float precio) {
        this.precio = precio;
    }

    public boolean isVendido() {
        return vendido;
    }

    public void setVendido(boolean vendido) {
        this.vendido = vendido;
    }
    
    public boolean getAnuncio(int id) throws SQLException{
         // JDBC driver name and database URL
      final String JDBC_DRIVER="com.mysql.jdbc.Driver";  
      final String DB_URL="jdbc:mysql://localhost:3306/java";

      //  Database credentials
      final String USER = "root";
      final String PASS = "";
      
      boolean correcto=false;
      Connection conn = null;
      Statement stmt= null;
      ResultSet rs= null;
       try{
         // Register JDBC driver
         Class.forName(JDBC_DRIVER);
         
         conn = DriverManager.getConnection(DB_URL,USER,PASS);
         stmt = conn.createStatement();
         rs = stmt.executeQuery("SELECT * FROM anuncios WHERE id="+id);
        if(rs.next()){
            setId(rs.getInt("id"));
            setPrecio(rs.getFloat("precio"));
            setTitulo(rs.getString("titulo"));
            setTexto(rs.getString("texto"));
            setVendido(rs.getBoolean("vendido"));
            
            correcto=true;
        }
        rs.close();
        stmt.close();
        conn.close();
        }catch (ClassNotFoundException ex) { 
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
         if(conn!=null){
           conn.close();
         }
         if(rs!=null){
           rs.close();
         }
         if(stmt!=null){
           stmt.close();
         }
       }
        return correcto;
    }
    
    public boolean saveAnuncio() throws SQLException{
       if(this.id!=0){
        return saveAnuncio(this.id,this.titulo, this.texto, this.precio, this.vendido);
    }else{
           return saveAnuncio(this.titulo, this.texto, this.precio, this.vendido);
       }
        
        
    }
    
    public boolean saveAnuncio(String titulo, String texto, float precio, boolean vendido) throws SQLException{
        
            // JDBC driver name and database URL
      final String JDBC_DRIVER="com.mysql.jdbc.Driver";  
      final String DB_URL="jdbc:mysql://localhost:3306/java";

      //  Database credentials
      final String USER = "root";
      final String PASS = "";
      
      boolean correcto=false;
      Connection conn = null;
      Statement stmt= null;
       try{
         // Register JDBC driver
         Class.forName(JDBC_DRIVER);
         
         conn = DriverManager.getConnection(DB_URL,USER,PASS);
         stmt = conn.createStatement();
        if(stmt.executeUpdate("INSERT INTO anuncios VALUES(default,'"+titulo+"','"+texto+"',"+precio+","+vendido+")")==1){
            correcto=true;
        }
        stmt.close();
        conn.close();
        }catch (ClassNotFoundException ex) { 
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
         if(conn!=null){
           conn.close();
         }
         if(stmt!=null){
           stmt.close();
         }
       }
        return correcto;
    }
    public boolean deleteAnuncio(int id) throws SQLException{
        
            // JDBC driver name and database URL
      final String JDBC_DRIVER="com.mysql.jdbc.Driver";  
      final String DB_URL="jdbc:mysql://localhost:3306/java";

      //  Database credentials
      final String USER = "root";
      final String PASS = "";
      
      boolean correcto=false;
      Connection conn = null;
      Statement stmt= null;
       try{
         // Register JDBC driver
         Class.forName(JDBC_DRIVER);
         
         conn = DriverManager.getConnection(DB_URL,USER,PASS);
         stmt = conn.createStatement();
        if(stmt.executeUpdate("DELETE FROM anuncios WHERE id="+id)==1){
            correcto=true;
        }
        stmt.close();
        conn.close();
        }catch (ClassNotFoundException ex) { 
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
         if(conn!=null){
           conn.close();
         }
         if(stmt!=null){
           stmt.close();
         }
       }
        return correcto;
    }
    public boolean saveAnuncio(int id,String titulo, String texto, float precio, boolean vendido) throws SQLException{
        
            // JDBC driver name and database URL
      final String JDBC_DRIVER="com.mysql.jdbc.Driver";  
      final String DB_URL="jdbc:mysql://localhost:3306/java";

      //  Database credentials
      final String USER = "root";
      final String PASS = "";
      
      boolean correcto=false;
      Connection conn = null;
      Statement stmt= null;
       try{
         // Register JDBC driver
         Class.forName(JDBC_DRIVER);
         
         conn = DriverManager.getConnection(DB_URL,USER,PASS);
         stmt = conn.createStatement();
        if(stmt.executeUpdate("UPDATE anuncios SET titulo='"+titulo+"',texto='"+texto+"',precio="+precio+",vendido="+vendido+" WHERE id="+id)==1){
            correcto=true;
        }
        stmt.close();
        conn.close();
        }catch (ClassNotFoundException ex) { 
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Anuncio.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
         if(conn!=null){
           conn.close();
         }
         if(stmt!=null){
           stmt.close();
         }
       }
        return correcto;
    }
}
